<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\api\bill;

use think\App;
use app\controller\property\Base;
use app\validate\bill\BillCatalogValidate;
use app\common\repositories\bill\BillCatalogRepository;

class BillCatalog extends Base
{
    protected $repository;

    public function __construct(App $app, BillCatalogRepository $repository)
    {
        parent::__construct($app);

        $this->repository = $repository;
    }


    /**
     *  确认收款
     * @param int $id
     * @return string|\think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-17 19:42
     */
    public function paymentAdd(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $param = $this->request->param([
            'pay_method' => '',
            'pay_amount' => '',
            'remark' => '',
        ]);
        validate(BillCatalogValidate::class)->scene('payment')->check($param);
        $info = $this->repository->get($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        if ($param['pay_amount'] > $info['stay_pay_amount']) {
            return $this->error('支付金额大于待支付金额');
        }
        if (!in_array($info['status'], [2, 3, 5, 8])) {
            return app('api_return')->error('此账单不支持此操作');
        }
        $res = $this->repository->paymentCustomBill($info, $param, $this->request->userId());
        if ($res) {
            return app('api_return')->success('确认成功');
        } else {
            return app('api_return')->error('确认失败');
        }
    }


}